package com.xiaobawang.xjsyy_idea_plugin.panel.config.view;

import com.intellij.openapi.ui.DialogWrapper;
import javax.swing.*;

import com.intellij.openapi.ui.Messages;
import com.xiaobawang.xjsyy_idea_plugin.global.GlobalConfig;
import org.jetbrains.annotations.Nullable;

import java.awt.*;

public class TokenDialog extends DialogWrapper {
    private JTextField myTokenField;

    public TokenDialog(TokenPanel.OnTokenUpdateListener listener) {
        super(true); // use current window as parent
        this.tokenUpdateListener = listener;
        setTitle("配置云效token");
        init();
    }

    // 添加一个构造函数参数来接收TokenPanel的回调监听器
    private final TokenPanel.OnTokenUpdateListener tokenUpdateListener;

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        JPanel myPanel = new JPanel(new BorderLayout());
        myTokenField = new JTextField();
        myPanel.add(new JLabel("请输入云效token: "), BorderLayout.NORTH);
        myPanel.add(myTokenField, BorderLayout.CENTER);
        return myPanel;
    }

    @Override
    protected void doOKAction() {
        String token = myTokenField.getText();
        if (!token.isEmpty()) {
            // token 有效
            GlobalConfig.token = token;
            if (tokenUpdateListener != null) {
                tokenUpdateListener.onTokenUpdated();
            }
            super.doOKAction();
        } else {
            // token 无效
            Messages.showErrorDialog("输入的Token不合法，请重新输入。", "错误");
        }
    }
}
